package org.lex.splitter.ui.awt;

import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.io.File;

import javax.swing.Box;

import org.apache.log4j.Logger;
import org.lex.splitter.ui.awt.actions.InputButtonAction;
import org.lex.splitter.ui.awt.actions.OutputButtonAction;
import org.lex.splitter.ui.awt.actions.RunButtonAction;
import org.lex.splitter.util.FileChooserUtils;

public class SplitContentPane extends Panel {
	private static final long serialVersionUID = 2760614856205121655L;
	private Logger log = Logger.getLogger(SplitContentPane.class);
	// ui
	private TextField inputTextField = null;
	private TextField outputTextField = null;
	private TextField sizeTextField = null;

	private Button inputButton = null;
	private Button outputButton = null;

	private Button runButton = null;

	public SplitContentPane() {
		this.initialize();
	}

	public File getInputFile() {
		return FileChooserUtils.getFile(this.getInputTextField().getText());
	}

	public void setInputFile(File inputFile) {
		String inputPath = "";
		File outputFolder = null;
		if (null != inputFile) {
			inputPath = inputFile.getAbsolutePath();
			outputFolder = inputFile.getParentFile();
		}
		this.getInputTextField().setText(inputPath);
		this.setOutputFolder(outputFolder);
	}

	public File getOutputFolder() {
		return FileChooserUtils.getFile(this.getOutputTextField().getText());
	}

	public void setOutputFolder(File outputFolder) {
		String outputPath = "";
		if (null != outputFolder) {
			outputPath = outputFolder.getAbsolutePath();
		}
		this.getOutputTextField().setText(outputPath);
	}

	public int getSplitSize() {
		return Integer.parseInt(this.getSizeTextField().getText());
	}

	public void setSplitSize(int size) {
		this.getSizeTextField().setText(Integer.toString(size));
	}

	private void initialize() {
		log.debug("init content pane");
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);

		GridBagConstraints c = null;
		{
			log.debug("init Input Lable");
			Label inputLabel = new Label("Input");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 0;
			c.gridy = 0;
			c.weightx = 0;
			c.weighty = 0;
			c.fill = GridBagConstraints.NONE;
			c.anchor = GridBagConstraints.BASELINE_LEADING;
			c.insets = new Insets(11, 11, 11, 5);
			this.add(inputLabel, c);
		}

		{
			log.debug("init Input TextField");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 1;
			c.gridy = 0;
			c.weightx = 10;
			c.weighty = 0;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.anchor = GridBagConstraints.BASELINE;
			c.insets = new Insets(11, 0, 11, 5);
			this.add(this.getInputTextField(), c);
		}
		{
			log.debug("init Input Button");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 2;
			c.gridy = 0;
			c.weightx = 0;
			c.weighty = 0;
			c.anchor = GridBagConstraints.BASELINE_TRAILING;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(11, 0, 11, 12);
			this.add(this.getInputButton(), c);
		}

		{
			log.debug("init Output Lable");
			Label outputLabel = new Label("Output");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 0;
			c.gridy = 1;
			c.weightx = 0;
			c.weighty = 0;
			c.fill = GridBagConstraints.NONE;
			c.anchor = GridBagConstraints.BASELINE_LEADING;
			c.insets = new Insets(0, 11, 11, 5);
			this.add(outputLabel, c);
		}

		{
			log.debug("init Output TextField");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 1;
			c.gridy = 1;
			c.weightx = 10;
			c.weighty = 0;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.anchor = GridBagConstraints.BASELINE;
			c.insets = new Insets(0, 0, 11, 5);
			this.add(this.getOutputTextField(), c);
		}
		{
			log.debug("init Output Button");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 2;
			c.gridy = 1;
			c.weightx = 0;
			c.weighty = 0;
			c.anchor = GridBagConstraints.BASELINE_TRAILING;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(0, 0, 11, 12);
			this.add(this.getOutputButton(), c);
		}
		{
			log.debug("init Size Label");
			Label sizeLabel = new Label("Size");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 0;
			c.gridy = 2;
			c.weightx = 0;
			c.weighty = 0;
			c.anchor = GridBagConstraints.BASELINE_LEADING;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(0, 11, 11, 5);
			this.add(sizeLabel, c);
		}
		{
			log.debug("init Size TextField");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 1;
			c.gridx = 1;
			c.gridy = 2;
			c.weightx = 0;
			c.weighty = 0;
			c.anchor = GridBagConstraints.BASELINE_LEADING;
			c.fill = GridBagConstraints.NONE;
			c.insets = new Insets(0, 0, 11, 12);
			this.add(this.getSizeTextField(), c);
		}

		{
			log.debug("init Run Button");
			c = new GridBagConstraints();
			c.gridheight = 1;
			c.gridwidth = 3;
			c.gridx = 0;
			c.gridy = 3;
			c.weightx = 0;
			c.weighty = 0;
			c.anchor = GridBagConstraints.CENTER;
			c.fill = GridBagConstraints.NONE;
			this.add(this.getRunButton(), c);
		}

		{
			log.debug("init Glue Pane");
			c = new GridBagConstraints();
			c.gridheight = 10;
			c.gridwidth = 3;
			c.gridx = 0;
			c.gridy = 4;
			c.weightx = 10;
			c.weighty = 10;
			c.anchor = GridBagConstraints.CENTER;
			c.fill = GridBagConstraints.BOTH;
			this.add(Box.createGlue(), c);
		}
	}

	private TextField getSizeTextField() {
		if (null == sizeTextField) {
			sizeTextField = new TextField("15360");
			sizeTextField.setColumns(8);
		}
		return sizeTextField;
	}

	private TextField getInputTextField() {
		if (null == inputTextField) {
			inputTextField = new TextField();
		}
		return inputTextField;
	}

	private TextField getOutputTextField() {
		if (null == outputTextField) {
			outputTextField = new TextField();
		}
		return outputTextField;
	}

	private Button getInputButton() {
		if (null == inputButton) {
			inputButton = new Button("File...");
			inputButton.addActionListener(new InputButtonAction());
		}
		return inputButton;
	}

	private Button getOutputButton() {
		if (null == outputButton) {
			outputButton = new Button("Folder...");
			outputButton.addActionListener(new OutputButtonAction());
		}
		return outputButton;
	}

	private Button getRunButton() {
		if (null == runButton) {
			runButton = new Button("Run");
			runButton.addActionListener(new RunButtonAction());
		}
		return runButton;
	}
}
